home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1996 #15
/
Monster Media Number 15 (Monster Media)(July 1996).ISO
/
prog_c
/
recio214.zip
/
TESTCHP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-14
|
6KB
|
221 lines
/* testchp.c - tests character delimited put functions */
/* recio version 2.14, release June 14, 1996 */
/* Copyright (C) 1994-1996, William Pierpoint */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include "recio.h"
/* errors to stderr */
#define errout stdout
enum {RESET, INCR, REPORT};
/****************************************************************************/
void /* return nothing */
warnttl( /* warning totals */
int action, /* action (0=reset, 1=increment, 2=report) */
int warnum) /* warning number */
/****************************************************************************/
{
static unsigned int widthcnt=0;
static unsigned int noregcnt=0;
static unsigned int timefaults=0;
switch (action) {
case RESET:
widthcnt=0;
timefaults=0;
return;
case INCR:
switch(warnum) {
case R_WWIDTH:
widthcnt++;
return;
case R_WNOREG:
noregcnt++;
return;
case R_WFAULT:
timefaults++;
return;
}
return;
case REPORT:
fprintf(errout, "\n");
if (widthcnt)
fprintf(errout, "WARNING -- %s -- %u times\n",
rstrwarning(R_WWIDTH), widthcnt);
if (noregcnt)
fprintf(errout, "WARNING -- %s\n", rstrwarning(R_WNOREG));
if (timefaults)
fprintf(errout, "WARNING -- Time conversion failed %u times\n",
timefaults);
return;
}
}
/****************************************************************************/
void /* returns nothing */
rwarnfn( /* recio callback warning function */
REC *rp) /* record pointer */
/****************************************************************************/
{
if (risvalid(rp)) warnttl(INCR, rwarning(rp));
}
/****************************************************************************/
void /* returns nothing */
errnofn( /* errno callback error function */
void) /* no parameters */
/****************************************************************************/
{
switch (errno) {
/* non-fatal errors */
case EACCES:
case EMFILE:
fprintf(errout, "ERROR: %s\n", strerror(errno));
break;
/* fatal errors (EINVAL, ENOMEM) */
default:
fprintf(errout, "FATAL ERROR: %s\n", strerror(errno));
exit(EXIT_FAILURE);
break;
}
}
/****************************************************************************/
void /* returns nothing */
rerrfn( /* recio callback error function */
REC *rp) /* record pointer */
/****************************************************************************/
{
if (risvalid(rp)) {
/* determine cause of error */
switch (rerror(rp)) {
case R_ENOPUT: /* could not write data */
fprintf(errout, "ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
break;
/* fatal errors (R_EINVMOD, R_EINVAL, R_ENOMEM) */
default:
fprintf(errout, "FATAL ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
exit(EXIT_FAILURE);
break;
}
/* invalid record pointer */
} else {
errnofn();
}
}
/****************************************************************************
main
*****************************************************************************/
int main()
{
int j, k; /* loop indices */
int i; /* integer field */
unsigned int ui; /* unsigned integer field */
long l; /* long field */
unsigned long ul; /* unsigned long field */
float f; /* float field */
double d; /* double field */
int ch; /* character field */
char *str = NULL; /* string field */
char str1[]="A"; /* string consisting of one letter */
struct tm t; /* broken-down time */
time_t time; /* time field */
/* install error and warning functions */
rinit(rerrfn, rwarnfn);
rsettmfmt(recout, "%m/%d/%Y %H:%M:%S");
/* if output not redirected */
if (isatty(fileno(stdout))) {
/* print instructions */
puts("TESTCHP version 2.14 Copyright (C) 1994-1996, William Pierpoint");
puts("Tests recio character delimited put functions.");
}
for (k=0; k<=1; k++) {
switch (k) {
case 1:
rsetfldch(recout, ',');
rsettxtch(recout, '"');
break;
}
/* initialize data */
i = -1;
ui = 1;
l = -1L;
ul = 1L;
f = 1.111111;
d = 1.111111111111111111111;
ch = 'a';
if (str) *str='\0';
*str1 = toupper(ch);
scpys(str, str1);
t = sftotm("1/1/1970 00:00:00", "%m/%d/%Y %H:%M:%S");
/* loop through data */
for (j=0; j<11; j++) {
rputi(recout, i);
rputui(recout, ui);
rputl(recout, l);
rputul(recout, ul);
rputf(recout, f);
rputd(recout, d);
rputc(recout, ch);
rputs(recout, str);
time=tmtotime(t);
/* if conversion error */
if (time == (time_t) -1 ) {
rsetwarn(recout, R_WFAULT);
}
rputt(recout, time);
i *= 10;
ui *= 10;
l *= 10L;
ul *= 10L;
f *= 10.;
d *= 10.;
ch += 1;
*str1 = toupper(ch);
scats(str, str1);
t.tm_year += 10;
rputrec(recout);
}
}
/* free dynamic string fields */
free (str);
/* check stream for warnings */
if (rwarning(recout)) warnttl(REPORT, NULL);
/* check stream for error */
if (rerror(recout)) {
fprintf(errout, "\nERROR %s -- %s\n",
rnames(recout), rerrstr(recout));
exit (EXIT_FAILURE);
}
return EXIT_SUCCESS;
}